package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.UserEngine;
/**
 * 用户业务实现类.
 * @author Gaara
 *
 */
public class UserEngineImpl extends BaseEngine implements UserEngine {

	@Override
	public String login(String username, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("password", password);
		JSONObject json = getJson(TYPE_POST, ContantValue.REDBOY_LOGIN_URI, map );
		if(json != null){
			String response = json.getString("response");
			if(response.equals("error")){
				JSONObject error = json.getJSONObject("error");
				return error.getString("text");
			}else if(response.equals("login")){
				//登录成功
				JSONObject userinfo = json.getJSONObject("userinfo");
				int userId = userinfo.getIntValue("userId");
				changeUserState(true, userId, username);
				return "登录成功,正在跳转...";
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	@Override
	public String regist(String username, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("password", password);
		JSONObject json = getJson(TYPE_POST, ContantValue.REDBOY_REGISTER_URI, map );
		if(json != null){
			String response = json.getString("response");
			if(response.equals("error")){
				JSONObject error = json.getJSONObject("error");
				return error.getString("text");
			}else if(response.equals("register")){
				//注册成功
				JSONObject userinfo = json.getJSONObject("userinfo");
				int userId = userinfo.getIntValue("userId");
				changeUserState(true, userId, username);
				return "注册成功,正在跳转...";
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	@Override
	public void changeUserState(boolean login,int userId, String username) {
		if(login){
			GloableParams.isLogin = true;
			GloableParams.userid = userId;
			GloableParams.username = username;
		}else{
			GloableParams.isLogin = false;
			GloableParams.userid = -1;
			GloableParams.username = "";
		}
	}

	@Override
	public JSONObject getUserInfo() {
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_USERINFO_URI, null, GloableParams.userid+"");
		if(json != null){
			String response = json.getString("response");
			if(response.equals("error")){
				return json.getJSONObject("error");
			}else if(response.equals("userinfo")){
				//获取成功
				return json.getJSONObject("userinfo");
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	@Override
	public JSONObject getFavoritesList(int page,int pageNum) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("pageNum", pageNum);
		//返回的字段中需要获取和判断的数据较多,不在此处处理了.
		return getJson(TYPE_GET, ContantValue.REDBOY_FAVORITES_URI, map, GloableParams.userid+"");
	}

	@Override
	public String cleanFavorites() {
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_FAVORITESCLEAN_URI, null, GloableParams.userid+"");
		//服务器返回数据成功{"response":"clean"}
		//失败:{"response":"error","error":{"text","..."}}
		if(json != null){
			String response = json.getString("response");
			if(response.equals("clean")){
				return "succeed";
			}else if(response.equals("error")){
				JSONObject error = json.getJSONObject("error");
				return error.getString("text");
			}
		}else{
			return null;
		}
		return null;
	}
	
}
